"""
@Time : 2021/9/16 15:07 
@Author : 常雷
@File : t_32装饰器控制频率.py 
@Software: PyCharm
"""
'''
写装饰器,限制函数被执行的频率,如10秒一次
'''
import time


def wrapper(func):
    start = 0
    def inner(*args, **kwargs):
        nonlocal start      # 内层修改外层变量
        now = time.time()
        if now - start > 10:
            start = now
            ret = func(*args, **kwargs)
            return ret
        else:
            print(f"对不起, 您访问的过于频繁, 请于{int(10-now+start)}秒之后访问")
    return inner


@wrapper
def han():
    print("我要打豆豆")

han()
han()
time.sleep(3)
han()
han()
han()
time.sleep(10)
han()